//页面加载完毕
window.onload=init

//监听鼠标移动事件
window.onmousemove = mouseMoveHandle;
var context;
var cW = 320,cH = 480;
//页面初始化
function init() {
	log('页面初始化');
	var canvas = document.getElementById('gameCanvas');
	context = canvas.getContext("2d");
	bg1 = createBgImg("rs/bg03.jpg");
	crtBg = bg1;
	bg2 = createBgImg("rs/bg03.jpg");
	nextBg = bg2;
	player = createBgImg("rs/ship01.png");
	//加载爆炸图片素材
	loadExplosionImgs();
	bgMusic =  loadAudio("rs/sfx/bgMusic.mp3");
//	bgMusic.play();
//	fireMusic =  loadAudio("rs/sfx/fireEffect.mp3");
//	fireMusic.load();
	explodeMusic =  loadAudio("rs/sfx/explodeEffect.mp3");
	setInterval(loop,1000/30);
}

var bgMusic,fireMusic,explodeMusic;
function createBgImg(src) {
	var bgTemp =  new Image();
	bgTemp.src = src;
	return bgTemp;
}
var count = 0;
function loop() {
	count++;
	//清屏
	clearScreen();
	//绘制背景
	drawBg();
	//绘制飞机
	drawPlayer();
	//玩家发射子弹
	if(count%5==0) {
		playerShoot(-10,10);
		playerShoot(0,0);
		playerShoot(10,10);
	}
	//绘制子弹
	drawPlayBullets();
	//生成敌人
	if(count%15==0) spawnEnemy();
	//绘制敌人
	drawEnemies();
	//碰撞检测
	hitTest();
	//绘制爆炸效果
	drawExplosion();
}

var explosionImgs = [];
//加载爆炸图片
function loadExplosionImgs() {
	for (var i = 1; i < 36; i++) {
		var n = i<10?"0"+i:i;
		var img = createBgImg("rs/explosion/explosion_"+n+".png");
		explosionImgs.push(img);
	}
}

//爆炸效果
var explosionArr = [];
function explode(x,y) {
	var explosion = {x:x,y:y,crtIndex:1};
	explosionArr.push(explosion);
}
//绘制爆炸效果
function drawExplosion() {
	for (var i = explosionArr.length -1; i >=0 ; i--) {
		var explosion = explosionArr[i];
		if(explosion.crtIndex>=35) {
			//爆炸效果播放完毕
			explosionArr.splice(i,1);
			continue;
		}
		var img = explosionImgs[explosion.crtIndex];
		context.drawImage(img,explosion.x-img.width*0.5,
							explosion.y-img.height*0.5);
		explosion.crtIndex++;
	}	
}

//检测碰撞
function hitTest() {
	testPlayerBulletAndEnemies();
}

//检查敌机跟子弹碰撞
function testPlayerBulletAndEnemies() {
	for (var i = 0; i < playerBullets.length; i++) {
		var bullet = playerBullets[i];
		for (var j = 0; j < enemys.length; j++) {
			var enemy = enemys[j];
			var hit = hitTestObject(enemy,bullet);
			if(hit) {
				playerBullets.splice(i,1);
				enemy.hp -= bullet.damage;
				if(enemy.hp<=0) {
					enemys.splice(j,1);
					explode(enemy.x,enemy.y);
					loadAudio("rs/sfx/explodeEffect.mp3").play();
					log("enemy died");
				}
				
				break;
			}
		}			
	}
}

function hitTestObject(obj1,obj2) {
	return hitTestPoint(obj1.x,obj1.y,obj1.img.width,obj1.img.height,obj2.x,obj2.y);
}
//判断检测方法
function hitTestPoint(x1,y1,w1,h1,x2,y2) {
	if(x2>=x1&&x2<=x1+w1 &&
	   y2>=y1&&y2<=y1+h1) return true;
	
	return false;
}

//绘制敌人
function drawEnemies() {
	for (var i = enemys.length-1;i>=0;i--) {
		var enemy = enemys[i];
		enemy.x += enemy.vx;
		enemy.y += enemy.vy;
		if(enemy.y>415) {
			enemys.splice(i,1); 
			continue;
		}
		context.drawImage(enemy.img,enemy.x,enemy.y);
	}
}
//敌人飞机
var enemys = [];
//生成敌机
function spawnEnemy() {
	var enemy = {};
	var i = parseInt(Math.random()*6);//随机生成飞机类型
	enemy.img = createBgImg("rs/Enemy/E"+i+".png");
	enemy.x = 320*Math.random();
	enemy.y = -50;
	enemy.vx = 0;
	enemy.vy = 1+5*Math.random();
	enemy.hp = 20+20*i;
	enemys.push(enemy);
}

//玩家飞机发射子弹
var playerBullets = [];//玩家子弹数组
function playerShoot(xOff,yOff) {
	loadAudio("rs/sfx/fireEffect.mp3").play();
	var bullet = {};
	bullet.img = createBgImg("rs/bullet/bullet01.png");
	bullet.x = playerX+14+xOff;
	bullet.y = playerY-14+yOff;
	bullet.vx = 0;
	bullet.vy = -8;
	//子弹伤害值
	bullet.damage = 50;
	//发射绘制
	playerBullets.push(bullet);
}

//绘制玩家子弹
function drawPlayBullets() {
	for (var i = playerBullets.length-1;i>=0;i--) {
		var bullet = playerBullets[i];
		bullet.x += bullet.vx;
		bullet.y += bullet.vy;
		if(bullet.y<5) {
			playerBullets.splice(i,1);
			continue;
		}
		context.drawImage(bullet.img,bullet.x,bullet.y);
	}
}

//-----------------------玩家飞机
var player;				//玩家
var playerWidth = 47.5; //玩家飞机宽度
var playerHeight = 54;  //玩家飞机高度
var playerX = 100,playerY = 300;    //玩家飞机坐标
var playerIndex = 0;    //玩家素材切割索引
function drawPlayer() {
	var playerClipx = playerWidth*playerIndex;
	context.drawImage(player,playerClipx,0,playerWidth,playerHeight,
		playerX,playerY,playerWidth,playerHeight);
	
	playerIndex = (playerIndex+1)%4;
}
//鼠标移动监听动作监听处理
function mouseMoveHandle(e) {
	playerX = e.x - playerWidth/2;
	playerY = e.y - playerHeight/2;
}
//-----------------------背景滚动
var bg1,bg2;
var bgMoveSpeed = 2;
var crtBgY = -160;
var crtBg,nextBg;
//绘画背景
function drawBg() {
	context.drawImage(crtBg,0,crtBgY);
	context.drawImage(nextBg,0,crtBgY-640);
	crtBgY += bgMoveSpeed;
//	log(crtBgY);
	if(crtBgY>cH) {
		if(crtBg == bg1) {
			crtBg = bg2;
			nextBg = bg1;
		}else {
			crtBg = bg1;
			nextBg = bg2;
		}
		crtBgY = crtBgY-640;
	}
}

//清除屏幕
function clearScreen() {
	context.clearRect(0,0,cW,cH);
}

function loadAudio(src) {
	var a = new Audio();
	a.src = src;
	return a;
}
//打印日志
function log(s) {
	console.log(s);
}
